<template class="ada">
  <div class="commodity-item">
    <ul>
      <li v-for="commodity in commodities" :key="commodity.id">
        <div class="commodity-item">
          <div class="commodity-details">
            <p><strong>商品名称:</strong> {{ commodity.commodityName }}</p>
            <p><strong>价格:</strong> {{ commodity.commodityPrice }}</p>
            <p><strong>积分:</strong> {{ commodity.tokens }}</p>
            <p hidden><strong>类型:</strong> {{ commodity.commodityType }}</p>
          </div>
          <button @click="buy(commodity.commodityType)">购买</button>
        </div>
      </li>
    </ul>
  </div>
  <a href="#" class="back-to-top" @click.prevent="$router.back()">
    <i class="fas fa-arrow-left"></i> 返回上一级
  </a>
</template>

<script setup>
import axios from 'axios';
import {ref, onMounted} from 'vue';

const commodities = ref([]);

const fetchCommodities = async () => {
  try {
    const response = await axios.get('/gpt/commodities');
    commodities.value = response.data;
  } catch (error) {
    console.error('Error fetching commodities:', error);
  }
};

onMounted(() => {
  fetchCommodities();
});

const buy = async (commodityType) => {
  console.log(`购买商品 ${commodityType}`);
  let user = localStorage.getItem("user");
  let userId = user.substr(11,19);
  console.log(userId);

  axios.post('/gpt/createOrder',
      {
        'userId':userId,
        'commodityType':commodityType,
      })
      .then((res) =>{//返回成功调用此方法
        //console.info(res);
        const newWindow = window.open('', '_blank');
        newWindow.document.write('<!DOCTYPE html><html><head><title>Payment</title></head><body>');
        newWindow.document.write(res.data); // 添加表单到新窗口
        newWindow.document.write('</body></html>');
        // document.querySelector('body').innerHTML = res.data;//查找到当前页面的body，将后台返回的form替换掉他的内容
        // document.forms[0].submit();  //执行submit表单提交，让页面重定向，跳转到支付宝页面
      })
      .catch((err)=>{
        console.log(err)
      })
};

  // 创建并提交表单
  const paymentForm = document.createElement('form');
  paymentForm.method = 'POST';
  paymentForm.action = formFields.action;

  for (const [key, value] of Object.entries(formFields)) {
    if (key !== 'action') {
      const input = document.createElement('input');
      input.type = 'hidden';
      input.name = key;
      input.value = value;
      paymentForm.appendChild(input);
    }
  }

  // 添加提交按钮并隐藏
  const submitButton = document.createElement('button');
  submitButton.type = 'submit';
  submitButton.style.display = 'none';
  paymentForm.appendChild(submitButton);

  // 将表单添加到当前组件的根元素
  const rootElement = document.querySelector('.commodity-item'); // 假设有一个类名为commodity-list的元素作为根
  rootElement.appendChild(paymentForm);

  // 提交表单
  paymentForm.submit();


};
</script>

<style scoped>
.ada{
  background-image: url('../../assets/background.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  min-height: 100vh;
}
.commodity-item {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s, box-shadow 0.2s;
  display: flex; /* 保持 */
  justify-content: center; /* 保持 */
  align-items: center; /* 保持 */
}

.commodity-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 6px 10px rgba(0, 0, 0, 0.15);
}

.commodity-item ul {
  list-style-type: none;
  padding: 0;
}

.commodity-item li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.commodity-item li:last-child {
  border-bottom: none;
}

.commodity-item .commodity-details {
  display: flex;
  flex-direction: column;
  align-items: flex-start; /* 保持 */
  text-align: left; /* 保持 */
  margin-right: 20px; /* 保持 */
}

.commodity-item p {
  margin: 0;
  padding: 0;
}

.commodity-item strong {
  font-weight: bold;
  color: #333;
}

.commodity-item button {
  padding: 8px 16px;
  font-size: 14px;
  color: #fff;
  background-color: #007bff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.commodity-item button:hover {
  background-color: #0056b3;
}

/* 新增样式 */
.back-to-top {
  display: inline-flex;
  align-items: center;
  margin-bottom: 10px;
  font-size: 16px;
  color: #007bff;
  cursor: pointer;
  transition: color 0.2s;
}

.back-to-top:hover {
  color: #0056b3;
}

.back-to-top i {
  margin-right: 5px;
}
</style>
